; ; @OnTest
; !2:: PasteText.Show()
; !3:: PasteText.Show(A_Clipboard)

#Include g:\AHK\git-ahk-lib\Theme.ahk

class PasteText extends Gui {
  static destroyed := true

  __New() {
    super.__New('-Caption +Border')
    this.SetFont('s12', 'consolas')
    headerBar := this.AddText('h20 ', '粘贴文本')
    headerBar.OnEvent('Click', (*) => PostMessage(0xA1, 2))
    edit := this.AddEdit('vEdit w650 h400')
    edit.GetPos(&ex, , &ew)
    btn := this.AddButton('yp Section w75', '确定')
    btn.OnEvent('Click', (*) => this.Send())
    this.AddButton('xp w75', '取消').OnEvent('Click', (*) => this.Des())
    btn.GetPos(&bx, , &bw)
    headerBar.Move(, , bx - ex + bw)
    this.AddStatusBar('vBar -Theme Background', 'init')
    ; fast edit
    this.AddButton('xp w75', '清空').OnEvent('Click', (*) => this['Edit'].Value := '')
    this.AddButton('xp w75', '读取').OnEvent('Click', (*) => this['Edit'].Value := A_Clipboard)
    this.AddButton('xp w75', '行号').OnEvent('Click', (*) => this._AddLineNum())
    Theme.Light(this)
  }

  SetStatus(text) {
    this['Bar'].Value := text
  }

  _AddLineNum() {
    group := this['Edit'].Value.split('`n')
    final := ''
    flag := false
    index := 1
    for v in group {
      if flag {
        flag := false
        continue
      }
      if !v {
        flag := true
      } else {
        flag := false
        final .= index <= 9 ? '0' : ''
        final .= index++ . ' ' . v
      }
      final .= '`r`n'
    }
    this['Edit'].Value := RTrim(final, '`r`n')
  }

  static AnimatedShow() {
    static ins ; singleton
    if (PasteText.destroyed) {
      ins := PasteText()
      this.destroyed := false
    } else {
      ins.Show()
      return
    }
    ins.Show('Minimize')
    WinSetTransparent(0, 'ahk_id' ins.Hwnd)
    ins.Restore()
    ins.GetClientPos(, , , &ch)
    WinSetTransparent(255, 'ahk_id' ins.Hwnd)
    loop ch {
      ins.Move(, , , A_Index)
    }
    ControlFocus(ins['Edit'], 'A')
  }

  static Show(data?) {
    if IsSet(data) && data {
      PasteText._PasteText(data)
    } else {
      PasteText.AnimatedShow()
    }
  }

  Des() {
    this.Destroy()
    PasteText.destroyed := true
  }

  Send(*) {
    data := this['Edit'].Value
    if (data) {
      this.GetClientPos(, , , &ch)
      while --ch > 0
        this.Move(, , , ch)
      this.Des()
      PasteText._PasteText(data)
    }
  }

  static _PasteText(data) {
    if (!data) {
      return
    }
    g := PasteGui()
    g.SetFont('s13', 'consolas')
    g.AddText('c4c071d', data)
    g.BackColor := 'f6eeda'
    g.Show('NA')
    WinSetTransColor('white', g)
    AddFrameShadow(g)
    g.RegisterEvent()
    Tip.ShowTip('粘贴一条文本喵~')
  }
}